layui.config({
	base : "js/"
}).use(['form','layer','jquery','laypage'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery;
 
	var apprData = '';
	$.ajax({
		url : '/admin/approval/show',
		type : 'get',
		dataType : 'json',
		success : function(data){
			apprData = data;
			if(window.sessionStorage.getItem('addAppr')){
				var addAgent = window.sessionStorage.getItem('addAppr');
				apprData = JSON.parse(addAppr).concat(apprData);			
			}			
			apprList();
			for(var i =0; i<apprData.length; i++){
　　             $("select[name ='id']").append("<option value='"+apprData[i].id+"'>" +apprData[i].name+ "</option>")  
                }
      		form.render();
		}
	});

	$(".search").click(function(){
		var newArray = [];
		console.log('id'+$("select[name=id]").val())
		if($("select[name=id]").val() != ''){
			var index = layer.msg('查询中，请稍候',{icon: 16,time:false,shade:0.8});
			setTimeout(function(){
				$.ajax({
					url : '/admin/approval/show',
					type : 'get',
					data : {'id':$("select[name=id]").val()},
					dataType : 'json',
					success : function(data){
						apprData = data;

						console.log(apprData);
						if(window.sessionStorage.getItem('addAppr')){
							var addAgent = window.sessionStorage.getItem('addAppr');
							apprData = JSON.parse(addAppr).concat(apprData);			
						}
						apprList();
					}
				})
			})
			layer.close(index);
		}
	})
	function apprList(that){
		var nums = 10;
		if (that) {
			apprData = that;
		}

		laypage({
			cont : 'page',
			pages : Math.ceil(apprData.length/nums),
			jump : function (obj) {
				$(".appr_content").html(renderDate(apprData,obj.curr));
				form.render();
			}
		})

		function renderDate (data,curr) {
			var dataHtml = '';
			if(!that){
				currData = apprData.concat().splice(curr*nums-nums, nums);
			}else{
				currData = that.concat().splice(curr*nums-nums, nums);
			}
			if(currData.length != 0){
				for(var i=0;i<currData.length;i++){
					dataHtml +='<tr>'
					+'<td>'+((curr-1)*nums+(i+1))+'</td>'
					+'<td>'+currData[i].account+'</td>'
					+'<td>'+currData[i].name+'</td>'
					+'<td>'+currData[i].linkman+'</td>'
					+'<td>'+currData[i].mobile+'</td>';
					if(currData[i].status == null || currData[i].status == ''){
						dataHtml += '<td class="audit">'
							+'<div class="layui-btn-group">'
                        		+'<button class="layui-btn layui-btn-danger layui-btn-mini links_del" appr="'+currData[i].id+'"><i class="layui-icon">&#x1006;</i> 拒绝 </button>'
                        		+'<button class="layui-btn layui-btn-mini links_edit" appr="'+currData[i].id+'"><i class="layui-icon">&#xe605;</i> 通过 </button>'
                    		+'</div>'
                    	+'</td>';
					}else{
						if(currData[i].status == 'pass'){
							dataHtml += '<td class="audit">已审核</td>';
						}
						if(currData[i].status == 'veto'){
							dataHtml += '<td class="audit">未通过</td>';
						}
					}
				}
			} else {
				dataHtml = '<tr><td colspan="7">暂无数据</td>';
			}
			dataHtml +='</tr>';
			return dataHtml;
		}
	}



	//审核
	$("body").on("click",".links_edit",function(){  //同意
		var _this = $(this).parents('td');
		var apprid = $(this).attr('appr');
		audit('pass',apprid,_this);
	})

	$("body").on("click",".links_del",function(){  //拒绝
		var _this = $(this).parents('td');
		var apprid = $(this).attr('appr');
		audit('veto',apprid,_this);
	})

    function audit (at,app,obj)
    {
	    $.ajax({
	        type:'GET',
	        url:'/admin/approval/audit',
	        data:{
	            status:at,
	            msg:app
	        },
	        dataType:'html',
	        success: function (raw)
	        {
	            if(raw == 1){
	                if(at == "veto" ){
						obj.html('未通过');
	                }
	                if(at == "pass"){
						obj.html('已审核');
	                }
	            }
	        }
	    });
    }

	
});


